<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class KefuController extends AdminController {

    /**
     * 后台首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('Member', $map);
        $weixin = M('Weixin')->field('jb_id,jb_title,nickname,openid')->select();
        $group = M('AuthGroup')->field('id,title')->select();
        foreach ($list as $key => $value) {
        	//循环组合用户会员信息
        	foreach ($weixin as $key => $o) {
        		if($o['openid'] == $value['openid']){
        			$value['wxname'] = $o['nickname'];	
         		} 
        	}
            foreach ($group as $key => $t) {
                    if($value['jb_id'] == $t['id']){
                        $value['jb_title'] = $t['title'];
                    }
               }   
        	$wxuser[] = $value;		
        }
        int_to_string($wxuser);
        $this->assign('_list', $wxuser);

        $this->meta_title = '客服管理';
        $this->display();
    }
    public function wxuser(){
    	$nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
           $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
           $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }
    	$list   = $this->lists('Weixin', $map);
        int_to_string($list);
    	$this->assign('_list', $list);

    	$this->meta_title = '微信用户';
    	$this->display();
    }
    //新增客服工号
  	public function add($username = '', $password = '', $repassword = '', $email = '',$jb ='',$openid =''){
        if(IS_POST){
            /* 检测密码 */
            
             if($password != $repassword){
                $this->error('密码和重复密码不一致！');
            }

            /* 调用注册接口注册用户 */
            $User   =   new UserApi;
            $uid    =   $User->register($username, $password, $email);
            if(0 < $uid){ //注册成功
	               	$user = array('uid' => $uid, 'nickname' => $username, 'status' => 1 ,'jb_id'=>$jb,'openid'=>$openid);
                    $data = array('uid' => $uid,'group_id' => $jb);
		                if(!M('Member')->add($user)){
		                    $this->error('用户添加失败！');
		                } else {
                            if(D('AuthGroupAccess')->add($data)){
                                $this->success('用户添加成功！',U('index'));
                            }		                    
		                }
		            } else { //注册失败，显示错误信息
		                $this->error($this->showRegError($uid));
		            }
        	} else {
        	$authgroup = D('AuthGroup')->where(array('description'=>1))->field('id,title')->select();
        	$this->assign('authgroup',$authgroup);
            $this->meta_title = '新增客服';
            $this->display();
        }
    }
    //修改工号信息
    public function edit(){
    	$uid   = I('uid');
    	$menber = M('member');
    	$edit  = $menber->where(array('uid'=>$uid))->field('uid,jb_id,nickname,openid')->find();
    	$authgroup = D('AuthGroup')->where(array('description'=>1))->field('id,title')->select();
    	$weixin =  M('weixin')->where(array('openid'=>$edit['openid']))->field('nickname')->find();
    	if(IS_POST){
    		$data['nickname']  = I('username');
    		$data['jb_id']     = I('jb');
    		$data['openid']    = I('openid');
    		$data['uid']       = I('uid');
            $group['uid']      = $data['uid'];
            $group['group_id'] = $data['jb_id'];
    		$res = $menber->where("uid = %d",array($data['uid']))->save($data);
    		if($res){
                D('AuthGroupAccess')->where(array('uid'=>$group['uid']))->save($group);
    			$this->success("更新权限成功！",U('index'));
    		}else{
    			$this->error("更新失败！");
    		}
    	}


    	$this->assign('authgroup',$authgroup);
    	$this->assign('weixin',$weixin);
    	$this->assign('edit',$edit);
    	$this->display('add');
    }
    //获取微信OPENID,添加方法使用
    public function getOpenId(){
    	$id = I('id');
    	$data = M('weixin')->where(array('jb_id'=>$id))->field('nickname,openid')->select();
    	$ajax = array();	
    	if(!$data){
    		$this->error("错误");
    	}

    	if(count($data) >= 2){
    		foreach ($data as $key => $value) {
    			$ajax[] = array('id'=>$value['openid'],'title'=>$value['nickname']); 
     		}
    	}else{
    		foreach ($data as $key => $value) {
    			$ajax = array('id'=>$value['openid'],'title'=>$value['nickname']); 
     		}
    	}

    	$this->ajaxReturn($ajax);
           
    }

    //显示用户用户分组
    public function group(){
    	$id            =   I('id');
        $auth_groups   =   D('AuthGroup')->where(array('description'=>1))->field('id,title')->select();
        $weixin        =   D('weixin')->field('id,jb_id,jb_title')->where(array('id'=>$id))->find();
      

        $this->assign('auth_groups',$auth_groups);
        $this->assign('user_groups',$weixin);
    	$this->meta_title = '客服授权';
    	$this->display();

    }
   	//用户分组提交数据
   	public function addToGroup($id = '',$group_id = ''){
   		$addgroup = M('weixin');
   		$title = D('AuthGroup')->where(array('id'=>$group_id))->field('id,title')->find();
   		$data = array('jb_id'=>$group_id,'jb_title'=>$title['title']);
   		if(IS_POST){

   			$res = $addgroup->where(array('id'=>$id))->save($data);
   			if($res){
   				$this->success('添加成功！');
   			}else{
   				$this->error('添加失败！');
   			}
   		}
   		

   	}


    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }

}
